{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn import tree\n",
    "from sklearn import preprocessing\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['RID', 'age', 'income', 'student', 'credit_rating', 'class_buys_computer']\n",
      "[{'income': 'high', 'age': 'youth', 'credit_rating': 'fair', 'student': 'no'}, {'income': 'high', 'age': 'youth', 'credit_rating': 'excellent', 'student': 'no'}, {'income': 'high', 'age': 'middle_aged', 'credit_rating': 'fair', 'student': 'no'}, {'income': 'medium', 'age': 'senior', 'credit_rating': 'fair', 'student': 'no'}, {'income': 'low', 'age': 'senior', 'credit_rating': 'fair', 'student': 'yes'}, {'income': 'low', 'age': 'senior', 'credit_rating': 'excellent', 'student': 'yes'}, {'income': 'low', 'age': 'middle_aged', 'credit_rating': 'excellent', 'student': 'yes'}, {'income': 'medium', 'age': 'youth', 'credit_rating': 'fair', 'student': 'no'}, {'income': 'low', 'age': 'youth', 'credit_rating': 'fair', 'student': 'yes'}, {'income': 'medium', 'age': 'senior', 'credit_rating': 'fair', 'student': 'yes'}, {'income': 'medium', 'age': 'youth', 'credit_rating': 'excellent', 'student': 'yes'}, {'income': 'medium', 'age': 'middle_aged', 'credit_rating': 'excellent', 'student': 'no'}, {'income': 'high', 'age': 'middle_aged', 'credit_rating': 'fair', 'student': 'yes'}, {'income': 'medium', 'age': 'senior', 'credit_rating': 'excellent', 'student': 'no'}]\n"
     ]
    }
   ],
   "source": [
    "# 读入数据\n",
    "Dtree = open(r'AllElectronics.csv', 'r')\n",
    "reader = csv.reader(Dtree)\n",
    "\n",
    "# 获取第一行数据\n",
    "headers = reader.__next__()\n",
    "print(headers)\n",
    "\n",
    "# 定义两个列表\n",
    "featureList = []\n",
    "labelList = []\n",
    "\n",
    "# \n",
    "for row in reader:\n",
    "    # 把label存入list\n",
    "    labelList.append(row[-1])\n",
    "    rowDict = {}\n",
    "    for i in range(1, len(row)-1):\n",
    "        #建立一个数据字典\n",
    "        rowDict[headers[i]] = row[i]\n",
    "    # 把数据字典存入list\n",
    "    featureList.append(rowDict)\n",
    "\n",
    "print(featureList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_data: [[0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]\n",
      " [0. 0. 1. 1. 0. 1. 0. 0. 1. 0.]\n",
      " [1. 0. 0. 0. 1. 1. 0. 0. 1. 0.]\n",
      " [0. 1. 0. 0. 1. 0. 0. 1. 1. 0.]\n",
      " [0. 1. 0. 0. 1. 0. 1. 0. 0. 1.]\n",
      " [0. 1. 0. 1. 0. 0. 1. 0. 0. 1.]\n",
      " [1. 0. 0. 1. 0. 0. 1. 0. 0. 1.]\n",
      " [0. 0. 1. 0. 1. 0. 0. 1. 1. 0.]\n",
      " [0. 0. 1. 0. 1. 0. 1. 0. 0. 1.]\n",
      " [0. 1. 0. 0. 1. 0. 0. 1. 0. 1.]\n",
      " [0. 0. 1. 1. 0. 0. 0. 1. 0. 1.]\n",
      " [1. 0. 0. 1. 0. 0. 0. 1. 1. 0.]\n",
      " [1. 0. 0. 0. 1. 1. 0. 0. 0. 1.]\n",
      " [0. 1. 0. 1. 0. 0. 0. 1. 1. 0.]]\n",
      "['age=middle_aged', 'age=senior', 'age=youth', 'credit_rating=excellent', 'credit_rating=fair', 'income=high', 'income=low', 'income=medium', 'student=no', 'student=yes']\n",
      "labelList: ['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']\n",
      "y_data: [[0]\n",
      " [0]\n",
      " [1]\n",
      " [1]\n",
      " [1]\n",
      " [0]\n",
      " [1]\n",
      " [0]\n",
      " [1]\n",
      " [1]\n",
      " [1]\n",
      " [1]\n",
      " [1]\n",
      " [0]]\n"
     ]
    }
   ],
   "source": [
    "# 把数据转换成01表示\n",
    "vec = DictVectorizer()\n",
    "x_data = vec.fit_transform(featureList).toarray()\n",
    "print(\"x_data: \" + str(x_data))\n",
    "\n",
    "# 打印属性名称\n",
    "print(vec.get_feature_names())\n",
    "\n",
    "# 打印标签\n",
    "print(\"labelList: \" + str(labelList))\n",
    "\n",
    "# 把标签转换成01表示\n",
    "lb = preprocessing.LabelBinarizer()\n",
    "y_data = lb.fit_transform(labelList)\n",
    "print(\"y_data: \" + str(y_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(class_weight=None, criterion='entropy', max_depth=None,\n",
       "            max_features=None, max_leaf_nodes=None,\n",
       "            min_impurity_split=1e-07, min_samples_leaf=1,\n",
       "            min_samples_split=2, min_weight_fraction_leaf=0.0,\n",
       "            presort=False, random_state=None, splitter='best')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建决策树模型\n",
    "model = tree.DecisionTreeClassifier(criterion='entropy')\n",
    "# 输入数据建立模型\n",
    "model.fit(x_data, y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_test: [0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]\n",
      "predict: [0]\n"
     ]
    }
   ],
   "source": [
    "# 测试\n",
    "x_test = x_data[0]\n",
    "print(\"x_test: \" + str(x_test))\n",
    "\n",
    "predict = model.predict(x_test.reshape(1,-1))\n",
    "print(\"predict: \" + str(predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'computer.pdf'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导出决策树\n",
    "# pip install graphviz\n",
    "# http://www.graphviz.org/\n",
    "import graphviz \n",
    "\n",
    "dot_data = tree.export_graphviz(model, \n",
    "                                out_file = None, \n",
    "                                feature_names = vec.get_feature_names(),\n",
    "                                class_names = lb.classes_,\n",
    "                                filled = True,\n",
    "                                rounded = True,\n",
    "                                special_characters = True)\n",
    "graph = graphviz.Source(dot_data)\n",
    "graph.render('computer')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"630pt\" height=\"552pt\"\r\n",
       " viewBox=\"0.00 0.00 630.00 552.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 548)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-548 626,-548 626,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.443137\" stroke=\"black\" d=\"M437.5,-544C437.5,-544 294.5,-544 294.5,-544 288.5,-544 282.5,-538 282.5,-532 282.5,-532 282.5,-473 282.5,-473 282.5,-467 288.5,-461 294.5,-461 294.5,-461 437.5,-461 437.5,-461 443.5,-461 449.5,-467 449.5,-473 449.5,-473 449.5,-532 449.5,-532 449.5,-538 443.5,-544 437.5,-544\"/>\r\n",
       "<text text-anchor=\"start\" x=\"290.5\" y=\"-528.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">age=middle_aged ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"311.5\" y=\"-513.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.9403</text>\r\n",
       "<text text-anchor=\"start\" x=\"322.5\" y=\"-498.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 14</text>\r\n",
       "<text text-anchor=\"start\" x=\"325.5\" y=\"-483.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [5, 9]</text>\r\n",
       "<text text-anchor=\"start\" x=\"330\" y=\"-468.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M352.5,-425C352.5,-425 247.5,-425 247.5,-425 241.5,-425 235.5,-419 235.5,-413 235.5,-413 235.5,-354 235.5,-354 235.5,-348 241.5,-342 247.5,-342 247.5,-342 352.5,-342 352.5,-342 358.5,-342 364.5,-348 364.5,-354 364.5,-354 364.5,-413 364.5,-413 364.5,-419 358.5,-425 352.5,-425\"/>\r\n",
       "<text text-anchor=\"start\" x=\"243.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">student=yes ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"258\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 1.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"256.5\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 10</text>\r\n",
       "<text text-anchor=\"start\" x=\"259.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [5, 5]</text>\r\n",
       "<text text-anchor=\"start\" x=\"267.5\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M343.102,-460.907C338.187,-452.195 332.942,-442.897 327.863,-433.893\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"330.82,-432.011 322.858,-425.021 324.723,-435.45 330.82,-432.011\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"316.146\" y=\"-445.403\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 12 -->\r\n",
       "<g id=\"node13\" class=\"node\"><title>12</title>\r\n",
       "<path fill=\"#399de5\" stroke=\"black\" d=\"M471,-417.5C471,-417.5 395,-417.5 395,-417.5 389,-417.5 383,-411.5 383,-405.5 383,-405.5 383,-361.5 383,-361.5 383,-355.5 389,-349.5 395,-349.5 395,-349.5 471,-349.5 471,-349.5 477,-349.5 483,-355.5 483,-361.5 483,-361.5 483,-405.5 483,-405.5 483,-411.5 477,-417.5 471,-417.5\"/>\r\n",
       "<text text-anchor=\"start\" x=\"391\" y=\"-402.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"393.5\" y=\"-387.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n",
       "<text text-anchor=\"start\" x=\"392.5\" y=\"-372.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 4]</text>\r\n",
       "<text text-anchor=\"start\" x=\"397\" y=\"-357.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;12 -->\r\n",
       "<g id=\"edge12\" class=\"edge\"><title>0&#45;&gt;12</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M389.245,-460.907C395.627,-449.763 402.559,-437.658 408.984,-426.439\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"412.075,-428.084 414.007,-417.667 406,-424.606 412.075,-428.084\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"420.563\" y=\"-438.092\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#e58139\" fill-opacity=\"0.749020\" stroke=\"black\" d=\"M261.5,-306C261.5,-306 160.5,-306 160.5,-306 154.5,-306 148.5,-300 148.5,-294 148.5,-294 148.5,-235 148.5,-235 148.5,-229 154.5,-223 160.5,-223 160.5,-223 261.5,-223 261.5,-223 267.5,-223 273.5,-229 273.5,-235 273.5,-235 273.5,-294 273.5,-294 273.5,-300 267.5,-306 261.5,-306\"/>\r\n",
       "<text text-anchor=\"start\" x=\"157.5\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">age=senior ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"156.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.7219</text>\r\n",
       "<text text-anchor=\"start\" x=\"171.5\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 5</text>\r\n",
       "<text text-anchor=\"start\" x=\"170.5\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [4, 1]</text>\r\n",
       "<text text-anchor=\"start\" x=\"178.5\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 1&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>1&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M269.122,-341.907C262.288,-332.923 254.979,-323.315 247.931,-314.05\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"250.664,-311.861 241.824,-306.021 245.092,-316.099 250.664,-311.861\"/>\r\n",
       "</g>\r\n",
       "<!-- 7 -->\r\n",
       "<g id=\"node8\" class=\"node\"><title>7</title>\r\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.749020\" stroke=\"black\" d=\"M474,-306C474,-306 304,-306 304,-306 298,-306 292,-300 292,-294 292,-294 292,-235 292,-235 292,-229 298,-223 304,-223 304,-223 474,-223 474,-223 480,-223 486,-229 486,-235 486,-235 486,-294 486,-294 486,-300 480,-306 474,-306\"/>\r\n",
       "<text text-anchor=\"start\" x=\"300\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">credit_rating=excellent ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"334.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.7219</text>\r\n",
       "<text text-anchor=\"start\" x=\"349.5\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 5</text>\r\n",
       "<text text-anchor=\"start\" x=\"348.5\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 4]</text>\r\n",
       "<text text-anchor=\"start\" x=\"353\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 1&#45;&gt;7 -->\r\n",
       "<g id=\"edge7\" class=\"edge\"><title>1&#45;&gt;7</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M330.878,-341.907C337.712,-332.923 345.021,-323.315 352.069,-314.05\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"354.908,-316.099 358.176,-306.021 349.336,-311.861 354.908,-316.099\"/>\r\n",
       "</g>\r\n",
       "<!-- 3 -->\r\n",
       "<g id=\"node4\" class=\"node\"><title>3</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M88,-179.5C88,-179.5 12,-179.5 12,-179.5 6,-179.5 0,-173.5 0,-167.5 0,-167.5 0,-123.5 0,-123.5 0,-117.5 6,-111.5 12,-111.5 12,-111.5 88,-111.5 88,-111.5 94,-111.5 100,-117.5 100,-123.5 100,-123.5 100,-167.5 100,-167.5 100,-173.5 94,-179.5 88,-179.5\"/>\r\n",
       "<text text-anchor=\"start\" x=\"8\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"10.5\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n",
       "<text text-anchor=\"start\" x=\"9.5\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [3, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"17.5\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;3 -->\r\n",
       "<g id=\"edge3\" class=\"edge\"><title>2&#45;&gt;3</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M155.142,-222.907C138.591,-210.88 120.502,-197.735 104.067,-185.791\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"105.787,-182.714 95.64,-179.667 101.672,-188.377 105.787,-182.714\"/>\r\n",
       "</g>\r\n",
       "<!-- 4 -->\r\n",
       "<g id=\"node5\" class=\"node\"><title>4</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M300,-187C300,-187 130,-187 130,-187 124,-187 118,-181 118,-175 118,-175 118,-116 118,-116 118,-110 124,-104 130,-104 130,-104 300,-104 300,-104 306,-104 312,-110 312,-116 312,-116 312,-175 312,-175 312,-181 306,-187 300,-187\"/>\r\n",
       "<text text-anchor=\"start\" x=\"126\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">credit_rating=excellent ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"173\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 1.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"175.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n",
       "<text text-anchor=\"start\" x=\"174.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 1]</text>\r\n",
       "<text text-anchor=\"start\" x=\"182.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;4 -->\r\n",
       "<g id=\"edge4\" class=\"edge\"><title>2&#45;&gt;4</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M212.388,-222.907C212.673,-214.558 212.977,-205.671 213.273,-197.02\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"216.771,-197.135 213.615,-187.021 209.775,-196.895 216.771,-197.135\"/>\r\n",
       "</g>\r\n",
       "<!-- 5 -->\r\n",
       "<g id=\"node6\" class=\"node\"><title>5</title>\r\n",
       "<path fill=\"#399de5\" stroke=\"black\" d=\"M192,-68C192,-68 116,-68 116,-68 110,-68 104,-62 104,-56 104,-56 104,-12 104,-12 104,-6 110,-0 116,-0 116,-0 192,-0 192,-0 198,-0 204,-6 204,-12 204,-12 204,-56 204,-56 204,-62 198,-68 192,-68\"/>\r\n",
       "<text text-anchor=\"start\" x=\"112\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"114.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"start\" x=\"113.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1]</text>\r\n",
       "<text text-anchor=\"start\" x=\"118\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 4&#45;&gt;5 -->\r\n",
       "<g id=\"edge5\" class=\"edge\"><title>4&#45;&gt;5</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M192.286,-103.726C187.459,-95.0615 182.353,-85.8962 177.498,-77.1802\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"180.475,-75.3322 172.55,-68.2996 174.36,-78.7389 180.475,-75.3322\"/>\r\n",
       "</g>\r\n",
       "<!-- 6 -->\r\n",
       "<g id=\"node7\" class=\"node\"><title>6</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M310,-68C310,-68 234,-68 234,-68 228,-68 222,-62 222,-56 222,-56 222,-12 222,-12 222,-6 228,-0 234,-0 234,-0 310,-0 310,-0 316,-0 322,-6 322,-12 322,-12 322,-56 322,-56 322,-62 316,-68 310,-68\"/>\r\n",
       "<text text-anchor=\"start\" x=\"230\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"232.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"start\" x=\"231.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"239.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 4&#45;&gt;6 -->\r\n",
       "<g id=\"edge6\" class=\"edge\"><title>4&#45;&gt;6</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M236.225,-103.726C240.735,-95.0615 245.506,-85.8962 250.043,-77.1802\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"253.153,-78.7859 254.666,-68.2996 246.944,-75.5537 253.153,-78.7859\"/>\r\n",
       "</g>\r\n",
       "<!-- 8 -->\r\n",
       "<g id=\"node9\" class=\"node\"><title>8</title>\r\n",
       "<path fill=\"#399de5\" stroke=\"black\" d=\"M424,-179.5C424,-179.5 348,-179.5 348,-179.5 342,-179.5 336,-173.5 336,-167.5 336,-167.5 336,-123.5 336,-123.5 336,-117.5 342,-111.5 348,-111.5 348,-111.5 424,-111.5 424,-111.5 430,-111.5 436,-117.5 436,-123.5 436,-123.5 436,-167.5 436,-167.5 436,-173.5 430,-179.5 424,-179.5\"/>\r\n",
       "<text text-anchor=\"start\" x=\"344\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"346.5\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n",
       "<text text-anchor=\"start\" x=\"345.5\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"350\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 7&#45;&gt;8 -->\r\n",
       "<g id=\"edge8\" class=\"edge\"><title>7&#45;&gt;8</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M387.959,-222.907C387.685,-212.204 387.388,-200.615 387.11,-189.776\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"390.606,-189.574 386.85,-179.667 383.608,-189.753 390.606,-189.574\"/>\r\n",
       "</g>\r\n",
       "<!-- 9 -->\r\n",
       "<g id=\"node10\" class=\"node\"><title>9</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M560,-187C560,-187 466,-187 466,-187 460,-187 454,-181 454,-175 454,-175 454,-116 454,-116 454,-110 460,-104 466,-104 466,-104 560,-104 560,-104 566,-104 572,-110 572,-116 572,-116 572,-175 572,-175 572,-181 566,-187 560,-187\"/>\r\n",
       "<text text-anchor=\"start\" x=\"462\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">age=youth ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"471\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 1.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"473.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n",
       "<text text-anchor=\"start\" x=\"472.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 1]</text>\r\n",
       "<text text-anchor=\"start\" x=\"480.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 7&#45;&gt;9 -->\r\n",
       "<g id=\"edge9\" class=\"edge\"><title>7&#45;&gt;9</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M432.021,-222.907C441.928,-213.56 452.55,-203.538 462.734,-193.929\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"465.183,-196.429 470.055,-187.021 460.379,-191.338 465.183,-196.429\"/>\r\n",
       "</g>\r\n",
       "<!-- 10 -->\r\n",
       "<g id=\"node11\" class=\"node\"><title>10</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M492,-68C492,-68 416,-68 416,-68 410,-68 404,-62 404,-56 404,-56 404,-12 404,-12 404,-6 410,-0 416,-0 416,-0 492,-0 492,-0 498,-0 504,-6 504,-12 504,-12 504,-56 504,-56 504,-62 498,-68 492,-68\"/>\r\n",
       "<text text-anchor=\"start\" x=\"412\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"414.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"start\" x=\"413.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"421.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 9&#45;&gt;10 -->\r\n",
       "<g id=\"edge10\" class=\"edge\"><title>9&#45;&gt;10</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M491.031,-103.726C486.362,-95.0615 481.424,-85.8962 476.727,-77.1802\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"479.767,-75.4428 471.942,-68.2996 473.604,-78.7632 479.767,-75.4428\"/>\r\n",
       "</g>\r\n",
       "<!-- 11 -->\r\n",
       "<g id=\"node12\" class=\"node\"><title>11</title>\r\n",
       "<path fill=\"#399de5\" stroke=\"black\" d=\"M610,-68C610,-68 534,-68 534,-68 528,-68 522,-62 522,-56 522,-56 522,-12 522,-12 522,-6 528,-0 534,-0 534,-0 610,-0 610,-0 616,-0 622,-6 622,-12 622,-12 622,-56 622,-56 622,-62 616,-68 610,-68\"/>\r\n",
       "<text text-anchor=\"start\" x=\"530\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">entropy = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"532.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"start\" x=\"531.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1]</text>\r\n",
       "<text text-anchor=\"start\" x=\"536\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 9&#45;&gt;11 -->\r\n",
       "<g id=\"edge11\" class=\"edge\"><title>9&#45;&gt;11</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M534.969,-103.726C539.638,-95.0615 544.576,-85.8962 549.273,-77.1802\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"552.396,-78.7632 554.058,-68.2996 546.233,-75.4428 552.396,-78.7632\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x17353cdb550>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['age=middle_aged',\n",
       " 'age=senior',\n",
       " 'age=youth',\n",
       " 'credit_rating=excellent',\n",
       " 'credit_rating=fair',\n",
       " 'income=high',\n",
       " 'income=low',\n",
       " 'income=medium',\n",
       " 'student=no',\n",
       " 'student=yes']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vec.get_feature_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['no', 'yes'], dtype='<U3')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lb.classes_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
